package com.bulkmail.sender;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlOptionsParserReader {
	private List<String> elements = new ArrayList<String>();

	public XmlOptionsParserReader(String path) {
		initialization(path);
	}

	private void initialization(String path) {
		try {

			File fXmlFile = new File(path);
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(fXmlFile);

			doc.getDocumentElement().normalize();

			NodeList nList = doc.getElementsByTagName("property");
			for (int temp = 0; temp < nList.getLength(); temp++) {
				Node nNode = nList.item(temp);

				if (nNode.getNodeType() == Node.ELEMENT_NODE) {
					Element elem = (Element) nNode;
					if (elem.getAttribute("value") == "")
						this.elements.add(elem.getTextContent());
					else
						this.elements.add(elem.getAttribute("value"));

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public DataSession populateDataSessionOptions(DataSession dataSession) {
		dataSession.setNrEmails(Integer.valueOf(elements.get(0)));
		dataSession.setSeconds(Integer.valueOf(elements.get(1)));
		dataSession.setAddCounter(Boolean.valueOf((elements.get(2))));
		dataSession.setAddRandomSubject(Boolean.valueOf(elements.get(3)));
		dataSession.setAttachPath(elements.get(4));
		dataSession.setSmtp(Boolean.valueOf(elements.get(5)));
		dataSession.setExs(Boolean.valueOf(elements.get(6)));

		return dataSession;

	}
}
